/**
 * @modal: 智能拆分策略
 */
import { CommonModal } from "@/pages/OrderModule/common/components/CommonModal";
import CommonTable from "@/components/CommonTable";
import { Button, Popconfirm, Space, message, Modal } from "antd";
import { useState, useEffect } from "react";
import AddSplittingStrategy from "../AddSplittingStrategy";
import CommonLoading from "@/components/CommonLoading";
import { updateOrderStatus } from "../../utils/orderStatus";
import Api from "../../service";
import { getOrderIdsOp } from "../../../../services";
import { checkOrder } from "../../utils/checkOrderConfig";

const strategyType = {
	1: "按商品数量拆分",
	2: "按退款状态拆分",
	3: "按供应商/供应商拆单",
	4: "按仓库拆单",
	5: "指定商品拆单",
	6: "按重量拆单"
};

function IntelligentSplittingStrategy(props) {
	const { item, selectKeys, orderList, searchForm, handleOk, handleCancel } = props;
	const { scene } = item;
	const [isOk, setIsOk] = useState(false);
	const [loading, setLoading] = useState(false);
	const [open, setOpen] = useState(false);
	const [dialogVisible, setDialogVisible] = useState();

	//策略列表
	const [strategyList, setStrategyList] = useState([]);
	const [currentId, setCurrentId] = useState();

	const strategyColumns = [
		{
			title: "策略名称",
			dataIndex: "name",
			render: t => {
				return t;
			}
		},
		{
			title: "策略类型",
			dataIndex: "type",
			render: t => {
				return strategyType[t];
			}
		},
		{
			title: "优先级",
			dataIndex: "level",
			render: t => {
				return t;
			}
		},
        {
			title: "参与订单",
			dataIndex: "participatingOrder",
			render: t => {
				return t == 1 ? "勾选订单": "筛选的符合条件订单";
			}
		},
		{
			title: "编辑策略",
			dataIndex: "id",
			width: 200,
			render: t => {
				return (
					<Space>
						<Button type="link" onClick={() => handleEdit(t)}>
							查看编辑
						</Button>
						<Popconfirm title="确认要删除该策略吗？" onConfirm={() => delStrategy(t)}>
							<Button danger type="link">
								删除
							</Button>
						</Popconfirm>
					</Space>
				);
			}
		},
		{
			title: "执行策略",
			dataIndex: "participatingOrder",
			render: (t, r) => {
				return (
					<Button type="link" onClick={() => runStrategy(t, r)}>
						执行策略
					</Button>
				);
			}
		}
	];

	useEffect(() => {
		setOpen(true);
		getStrategyList();
	}, []);

	//获取策略列表
	const getStrategyList = async () => {
		setLoading(true);
		const { success, data } = await Api.spitStrategyList();
		if (success) {
			setStrategyList(data);
		}
		setLoading(false);
	};

	//编辑策略
	const handleEdit = val => {
		setCurrentId(val);
		setDialogVisible(true);
	};

	//删除策略
	const delStrategy = async val => {
		setLoading(true);
		const { success } = await Api.delSpitStrategy({ id: val });
		setLoading(false);
		if (success) {
			message.success("删除成功");
			getStrategyList();
		}
	};

	//点击执行策略
	const runStrategy = async (t, r) => {
		if (t == 1) {
			//针对选择的订单
			if (selectKeys.length > 0) {
				if (checkOrder(scene, selectKeys, orderList)) {
					handleRunStrategy(r.id, selectKeys);
				}
			} else {
				message.info("请勾选订单");
			}
		} else {
			//针对筛选订单并且是待审核的订单
			setLoading(true);
			const params = {
				...searchForm,
				stateArr: [10]
			}
			const { data, success } = await getOrderIdsOp(params);
			setLoading(false);
			if (success) {
				if (!data.auditCount.length > 0) {
					handleRunStrategy(r.id, data.orderList);
				} else {
					Modal.info({ centered: true, title: "提示信息", content: "无可操作订单" });
				}
			}
		}
	};

	//执行策略
	const handleRunStrategy = (id, orderIds) => {
		setLoading(true);
		updateOrderStatus(
			scene,
			{
				orderIds,
				strategyId: id
			},
			val => {
				setLoading(false);
				if (val) {
					setIsOk(true);
				}
			}
		);
	};

	const onCancel = () => {
		setOpen(false);
	};

	const afterClose = () => {
		if (isOk) {
			// handleOk();
			handleCancel();
		} else {
			handleCancel();
		}
	};

	const TableProps = {
		data: strategyList,
		bordered: true,
		columns: strategyColumns,
		scroll: {
			y: "50vh"
		}
	};

	const DialogProps = {
		id: currentId,
		handleOk() {
			getStrategyList();
			setCurrentId();
			setDialogVisible(false);
		},
		handleCancel() {
			setDialogVisible(false);
			setCurrentId();
		}
	};

	const modalFooter = <Button onClick={onCancel}>关闭</Button>;

	return (
		<CommonModal
			title={{ title: "智能拆分策略" }}
			footer={modalFooter}
			afterClose={afterClose}
			open={open}
			width="747px"
			onCancel={onCancel}
		>
			<Button style={{ marginBottom: "16px" }} onClick={() => setDialogVisible(true)}>
				新增策略
			</Button>
			<CommonTable {...TableProps} />
			{dialogVisible && <AddSplittingStrategy {...DialogProps} />}
			<CommonLoading loading={loading} />
		</CommonModal>
	);
}

export default IntelligentSplittingStrategy;
